import java.text.DateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;

/**
 * Calendar Clone bug. Execute with -ea!
 * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7192782
 */
public class CalendarBug
{
    private static final DateFormat DATE_FORMAT = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);

    public static void main(String[] args)
    {
        try
        {
            assert false;
            throw new IllegalStateException("Please enabled assertions using the -ea switch");
        }
        catch (AssertionError e)
        {
            // ok, assertions are on
        }

        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("CET"), Locale.US);

        // define some date, e.g. the 1st December (day 336 of year)
        calendar.set(2012, 11, 1, 22, 26);

        // clone the calendar
        Calendar first = (Calendar) calendar.clone();

        // set the day of the year to the day before, e.g. 30th November
        first.set(Calendar.DAY_OF_YEAR, 335);

        // if the first calendar is used, internal values are calculated
        // if you uncomment the following line, the assertions are correct 
        // first.getTime();

        // clone another time
        Calendar second = (Calendar) first.clone();

        // update to the first of day
        second.set(Calendar.DAY_OF_MONTH, 1);

        assert "12/1/12".equals(DATE_FORMAT.format(calendar.getTime())) : "Expected 12/1/12 but got "
            + DATE_FORMAT.format(calendar.getTime());
        assert "11/30/12".equals(DATE_FORMAT.format(first.getTime())) : "Expected 11/30/12 but got "
            + DATE_FORMAT.format(first.getTime());
        assert "11/1/12".equals(DATE_FORMAT.format(second.getTime())) : "Expected 11/1/12 but got "
            + DATE_FORMAT.format(second.getTime());
    }
}
